package org.springblade.mes.log.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.mes.log.entity.SyncHistory;
import org.springblade.mes.log.mapper.SyncHistoryMapper;
import org.springblade.mes.log.model.SyncHistoryBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 服务
 *
 * @Version 1.0
 */
@Slf4j
@Service
public class SyncHistoryService extends BaseServiceImpl<SyncHistoryMapper, SyncHistory> {

    @Transactional(rollbackFor = Exception.class)
    public boolean addData(SyncHistoryBean param) throws Exception {
        //保存
        SyncHistory entity = param;
        return this.save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateData(SyncHistoryBean param) throws Exception {
        //更新
        SyncHistory newEntity = param;
        return this.updateById(newEntity);
    }


    @Transactional(rollbackFor = Exception.class)
    public boolean deleteData(SyncHistoryBean param) throws Exception {
        if (param.getIdList() != null && param.getIdList().size() > 0) {
            UpdateWrapper<SyncHistory> del = new UpdateWrapper<>();
            del.lambda().in(SyncHistory::getId, param.getIdList());
            return this.remove(del);
        }
        if (param.getId() != null) {
            return this.removeById(param.getId());
        }
        return true;
    }


    public SyncHistoryBean findById(Long id) {
        SyncHistoryBean cond = new SyncHistoryBean();
        cond.setId(id);
        return findBySpec(cond);
    }


    public SyncHistoryBean findBySpec(SyncHistoryBean param) {
        List<SyncHistoryBean> list = this.baseMapper.queryList(param);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }


    public List<SyncHistoryBean> findListBySpec(SyncHistoryBean param) {
        return this.baseMapper.queryList(param);
    }


    public IPage<SyncHistoryBean> findPageBySpec(IPage<SyncHistoryBean> page, SyncHistoryBean param) {
        List<SyncHistoryBean> dataList = baseMapper.queryPageList(page, param);
        return page.setRecords(dataList);
    }

    /**
     * 字典赋值
     */
    private List<SyncHistoryBean> fillDict(List<SyncHistoryBean> dataList) {
        if (dataList == null || dataList.size() == 0) {
            return Collections.emptyList();
        }
        for (SyncHistoryBean vo : dataList) {
            //TODO
        }
        return dataList;
    }
}

